package controllers.interceptors

import com.jfinal.core.Controller
import com.jfinal.validate.Validator
import base.Message
import base.State
import controllers.UserController

class PhoneValidator: Validator() {

    companion object{
        val TAG = PhoneValidator::class.simpleName!!
    }

    override fun validate(c: Controller) {
        val phone = getController().get(UserController.PARA_PHONE)
//        LogUtil.i(TAG, "phone=$phone")
        val regex = Regex("^(1[0-9][0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\\d{8}\$")
        if (phone == null || !regex.matches(phone)){
            addError(UserController.PARA_PHONE, State.ERROR_USER_INVALID_PHONE)
        }
    }

    override fun handleError(c: Controller) {
        val message = Message(State.STATE_FAIL, data = State.getEorrorStr(State.ERROR_USER_INVALID_PHONE))
        c.renderJson(message)
    }
}